package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"message-queue_0826/api/params"
	"message-queue_0826/pkg"
	"net/http"
	"strconv"
	"time"
)

func AddQueue(c *gin.Context) {

	var req params.AddQueueForm

	err := c.ShouldBindJSON(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	rabbitmq := pkg.NewRabbitMQSimple("" +
		"kuteng")
	rabbitmq.PublishSimple(req.Content)

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "发送成功",
		"data":    "",
	})
	fmt.Println("发送成功！")
}

func AddPublishQueue(c *gin.Context) {

	var req params.AddQueueForm

	err := c.ShouldBindJSON(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "发送成功",
		"data":    "",
	})
	fmt.Println("发送成功！")

	rabbitmq := pkg.NewRabbitMQPubSub("" +
		"newProduct")
	for i := 0; i < 100; i++ {
		rabbitmq.PublishPub("订阅模式生产第" +
			strconv.Itoa(i) + "条" + "数据")
		fmt.Println("订阅模式生产第" +
			strconv.Itoa(i) + "条" + "数据")
		time.Sleep(1 * time.Second)
	}

}
